package com.tf_cut.backend.platform.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.mapper.ProfitMapper;
import com.tf_cut.backend.platform.service.ProfitService;
import com.tf_cut.backend.platform.vo.SellerAccount;
import com.tf_cut.backend.util.CalendarUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional
@Slf4j
public class ProfitServiceimpl implements ProfitService{
	
	@Autowired
	private ProfitMapper profitMapper;

	@Override
	public PageInfo<SellerAccount> queryByProfit(int page, int rows, String sellerName, String startDate, String endDate) {
		com.github.pagehelper.PageHelper.startPage(page,99999);
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
    	if(!endDate.equals("") && endDate != null){
    		String num = endDate.substring(0, 4);
            String strh = endDate.substring(5,7);
            
            endDate= df.format(CalendarUtil.getLastDayOfMonth(Integer.valueOf(num).intValue(),Integer.valueOf(strh).intValue()));
    	}
    	if(!startDate.equals("") && startDate != null){
    		startDate= startDate + "-01 00:00:00";
    	}
    	
		
        List<SellerAccount> list = profitMapper.queryByProfit(sellerName,startDate,endDate);
        com.github.pagehelper.PageInfo<SellerAccount> pageInfo = new com.github.pagehelper.PageInfo<>(list);

        PageInfo<SellerAccount> returnPageInfo = new PageInfo<>();
        returnPageInfo.setPage(pageInfo.getPageNum());
        returnPageInfo.setPageSize(pageInfo.getPageSize());
        returnPageInfo.setRows(pageInfo.getList());
        returnPageInfo.setRecords((int) pageInfo.getTotal());
        returnPageInfo.setTotal(pageInfo.getPages());
        return returnPageInfo;
	}

}
